<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/common.css" media="all">
    <script src="/js/layui/layui.all.js"></script>
   <!-- <script src="/admin/js/token.js"></script>-->
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div style="text-align: center;">
                <div class="layui-inline">
                    <input class="layui-input" name="id" id="test-table-demoReload" placeholder="输入昵称或账号" autocomplete="off">
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin search" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>搜索
                    </button>
                </div>
            </div>
        </div>



        <div class="layui-card-body">
            <table class="layui-hide" id="t" lay-filter="t"></table>

        </div>

    </div>
</div>



</body>

<script type="text/html" id="tb">
    <div class="layui-container" style="margin-left:0px;">
        <a class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-addition"></i>新增管理员</a>
    </div>
</script>

<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="status" value="{{d.id}}" title="启用" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="caozuo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">角色编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="chongzhi">重置密码</a>
</script>

<script>
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#t',
            id: 'tt',
            toolbar: '#tb',
            url: '/admin/role/adminRole'
            ,
            cols: [
                [{
                    field: 'id',
                    title: '序号',
                    width: '13%',
                    type:'numbers',
                    align: 'center'
                },
                    {
                        field: 'username',
                        title: '管理员账号',
                        width: '30%',
                        align: 'center'
                    },

                    {
                        field: 'role_name',
                        title: '角色',
                        width: '30%',
                        align: 'center'
                    },
                    {fixed: 'right',field: 'status',align : 'center', title:'状态', templet: '#checkboxTpl', sort: true},
                    {
                        title: '操作',
                        align: 'center',
                        toolbar: '#caozuo',
                        fixed: 'right'
                    }
                ]
            ],
            limit:40,
            page: true
        });

        //监听用户状态
        form.on('checkbox(lockDemo)', function(obj){
            var id = this.value;
            if(obj.elem.checked){
                var status=1;
            }else{
                var status=0;
            }
            $.ajax({
                url:"/login/updAdminStatus"
                ,type:"GET"
                ,data:{id : id,status : status}
                ,contentType: 'application/json'
                ,success:function (data) {
                    if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('操作成功');
                        table.reload('tt');
                        // location.reload(true);
                    }else if(data.code==1){
                        layer.msg(data.msg);
                    }else{
                        layer.msg("拒绝访问");
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
        });

        //监听工具条
        table.on('tool(t)', function (obj) {
            var data = obj.data
            if (obj.event === 'detail') {
                layer.open({
                    type: 2,
                    area: ['100%', '100%'],
                    content: 'editrole.html?adminId='+data.id, //这里content是一个普通的String
                    end: function(){
                        location.reload();
                    }
                });
            }else if(obj.event === 'chongzhi'){
                layer.confirm("确定重置密码吗?",{icon: 3, title:'提示'},function (index) {
                    $.ajax({
                        type:"post",
                        url:"/user/resetPassword",
                        data:{
                            id:data.id
                        },
                        dataType:"json",
                        success:function (res) {
                            if(res.code=200){
                                layer.msg(res.msg);
                            }else{
                                layer.msg("操作失败");
                            }
                        }
                    })
                })
            }
        });

        table.on('toolbar(t)', function (obj) {
            var data = obj.data;
            // console.log(data);
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    area: ['100%', '100%'],
                    content: 'add_user.html', //这里content是一个普通的String
                    end: function(){
                        location.reload();
                    }
                });
            }
        });

        $('.test-table-operate-btn .layui-btn').on('click', function () {
            var type = $(this).data('type')
            active[type] ? active[type].call(this) : ''
        });
        
        $('.search').on('click',function () {
            var text = $('#test-table-demoReload').val();
            table.reload('tt',{
                url:'/admin/role/adminRole',
                where:{
                  text:text
                },
                page:{
                    curr:1
                }
            })
        })
</script>
</html>
